"""
===================
ballista.exceptions
===================

A few ``Exception`` subclasses.

"""

import os
import time

from ballista.util import rfc3339

class BallistaException(Exception):
    """
    BallistaException
    =================

    The basic ``Exception`` subclass which all other builtin exceptions subclass themselves. This automatically handles logging unless specifically requested not to (through the environment variable ``BALLISTA_DISABLE_LOGGING``).
    
    """
    def __init__(self, *args):
        """
        BallistaException.__init__
        ==========================

        __init__(self, *args) -> BallistaException instance
        Initialise the exception and log whatever happened.
        
        """
        self.args = args
        if not os.environ.get('BALLISTA_DIABLE_LOGGING'):
            now = rfc3339()
            logfile = open(os.environ.get('BALLISTA_LOGFILE', './error.log'), 'a')
            logfile.write('[%s] [%s] [%s] %s\n' % (now, self.__class__.__name__, os.environ.get('REMOTE_ADDR', '?'), self.args))

class IncompleteResponse(BallistaException):
    pass

class InvalidRoute(BallistaException):
    pass

class NoMatchingRoute(BallistaException):
    pass

class UnexpectedResponse(BallistaException):
    pass
